package com.magic.item.controller;

import com.magic.common.pojo.PageResult;
import com.magic.item.pojo.Brand;
import com.magic.item.pojo.Category;
import com.magic.item.service.BrandService;
import com.magic.item.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.ArrayList;
import java.util.List;


/**
 * @author lan.jianyuan
 * @create 2021-01-06-18:28
 */
@Controller
@RequestMapping("brand")
public class BrandController {
    @Autowired
    private BrandService brandService;
    @Autowired
    private CategoryService categoryService;
    /**
     *根据查询条件分页并排序查询品牌信息
     * @param key
     * @param page
     * @param rows
     * @param sortBy
     * @param desc
     * @return
     */
    @GetMapping("page")
    public ResponseEntity<PageResult<Brand>> queryBrandByPage(
            @RequestParam(value = "key", required = false) String key,
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "rows", defaultValue = "5") Integer rows,
            @RequestParam(value = "sortBy", required = false) String sortBy,
            @RequestParam(value = "desc", required = false) boolean desc
    ) {
        PageResult<Brand> result = this.brandService.queryBrandByPageAndSort(page, rows, sortBy, desc, key);
        if (CollectionUtils.isEmpty(result.getItems())) {
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(result);
    }
    /**
     * 新增品牌
     * @param brand
     * @param cids
     * @return
     */
    @PostMapping
    public ResponseEntity<Void> saveBrand(Brand brand, @RequestParam("cids") List<Long> cids) {
        this.brandService.saveBrand(brand, cids);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }
    
    /**
     * 根据品牌信息查询商品分类
     * @param bid
     * @return
     */
    @GetMapping("bid/{id}")
    public ResponseEntity<List<Brand>> selectUpdateBrand(@PathVariable("id")Long bid) {
        List<Brand> brands = this.brandService.selectUpdateBrand(bid);
        List<Category> categories = new ArrayList<>();
        brands.forEach(brand -> {
            List<Long> categoryBrandIds = this.brandService.selectCategoryId(bid);
            for (Long categoryBrandId : categoryBrandIds) {
                List<Category> categories1 = this.categoryService.queryCategoryId(categoryBrandId);
                categories.add(categories1.get(0));
                if (categories.size() ==  1) {
                    categories1.forEach(category -> {
                        brand.setName(category.getName());
                    });
                }
                else {
                    for (Category category : categories) {
                        String name = category.getName();
                        String names = "," + name;
                        for (Category category1 : categories) {
                            if (!category1.getName().equals(name)) {
                                String categoryName = category1.getName().concat(names);
                                brand.setName(categoryName);
                            }
                        }
                    }
                }
            }
        });
        if (CollectionUtils.isEmpty(brands)) {
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(brands);
    }
    /**
     * 修改品牌
     * @param brand
     * @param cids
     * @return
     */
    @PutMapping
    public ResponseEntity<Void> updateBrand(Brand brand, @RequestParam("cids")List<Long> cids) {
        this.brandService.updateBrand(brand, cids);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }
    /**
     * 删除品牌
     * @param bid
     */
    @GetMapping("delete/{id}")
    public ResponseEntity<Void> deleteBrand(@PathVariable("id")Long bid) {
        this.brandService.deleteBrand(bid);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }
    /**
     * 根据分类id查询品牌列表
     * @param cid
     * @return
     */
    @GetMapping("cid/{cid}")
    public ResponseEntity<List<Brand>> queryBrandByCid(@PathVariable("cid")Long cid) {
        List<Brand> brands = this.brandService.queryBrandsByCid(cid);
        if (CollectionUtils.isEmpty(brands)) {
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(brands);
    }
    
    /**
     * 根据分类id查询品牌名称
     * @param id
     * @return
     */
    @GetMapping("{id}")
    public ResponseEntity<Brand> queryBrandById(@PathVariable("id")Long id) {
        Brand brand = this.brandService.queryBrandById(id);
        if (brand == null) {
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(brand);
    }
}
